﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.OleDb;
using ContactManager.Common;

namespace ContactManager
{
    public partial class LoginFrm : AlSkin.AlForm.AlBaseForm
    {
        public LoginFrm()
        {
            this.AcceptButton = this.btnLogin;
            this.CancelButton = this.btnExit;
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = this.txtUserName.Text.Trim();
            string userpass = this.txtUserPass.Text.Trim();


            if (username == "")
            {
                MessageBox.Show("请输入用户名");
                return;
            }
            if (userpass == "")
            {
                MessageBox.Show("请输入密码");
                return;
            }

            string loginSql = "select count(*) from users t where t.user_name = '" + username + "' and t.user_pass = '" + userpass + "'";
            if (AccessHelper.ExecuteScalar(loginSql) > 0)
            {
                //记住帐号，密码判断
                string name = this.txtUserName.Text.Trim();
                string pass = this.txtUserPass.Text.Trim();
                string sql = "update remeberme set username=?,userpass=? where id=1";
                if (!this.checkBoxName.Checked)
                {
                    name = "";
                }
                
                if (!this.checkBoxPassword.Checked)
                {
                    pass = "";
                }
                OleDbParameter[] ps = {
                                        new OleDbParameter("@username",name),
                                        new OleDbParameter("@userpass",pass)
                                      };
                AccessHelper.ExecuteNonQuery(sql,ps);

                //登录成功
                GlobleUser.USER_NAME = username;
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.None;
                MessageBox.Show("抱歉你输入的用户名或密码错误");
                this.txtUserPass.Text = "";
                this.txtUserName.Focus();
            }
            
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            RegisterFrm rf = new RegisterFrm(this);
            rf.Show();
        }

        //从外界来设置用户名
        public void setUserName(string username)
        {
            this.txtUserName.Text = username;
        }

        private void LoginFrm_Load(object sender, EventArgs e)
        {
            //设置窗口位置
            this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, 0);


            //查找记住的帐号和密码
            string sql = "select * from remeberme where id = 1";
            DataSet ds = AccessHelper.ExecuteQuery(sql);
            DataRow dr = ds.Tables[0].Rows[0];
            if (dr[1] != null && dr[1].ToString().Length > 0)
            {
                this.checkBoxName.Checked = true;
                this.txtUserName.Text = dr[1].ToString();
            }
            if (dr[2] != null && dr[2].ToString().Length > 0)
            {
                this.checkBoxPassword.Checked = true;
                this.txtUserPass.Text = dr[2].ToString();
            }
        }

        private void LoginFrm_Activated(object sender, EventArgs e)
        {
            this.txtUserName.Focus();
        }



    }
}
